package design_pattern.observer.jdkObserver;

import java.util.HashSet;

/**
 * @version 1.0.0
 * Created by cyd on 2022-08-31 23:36:31
 * @ClassName : design_pattern.observer.jdkObserver.Example03
 * @Description : 类描述
 */
public class TestObserver {

    /**
     * 这里是羊监听狼:在狼没有出来捕猎的时候，羊的状态是睡觉，在狼出来捕猎的时候，羊的状态改为逃命
     */
    public static void main(String[] args) {
        Wolf wolf = new Wolf("wolf");
        Sheep sheep1 = new Sheep("sheep1");
        Sheep sheep2 = new Sheep("sheep2");
        Sheep sheep3 = new Sheep("sheep3");
        // 注册观察者,sheep1,sheep2加入，sheep3未加入
        wolf.addObserver(sheep1);
        wolf.addObserver(sheep2);

        HashSet<Sheep> set = new HashSet<>();
        set.add(sheep1);
        set.add(sheep2);
        set.add(sheep3);

        System.out.println("--------------羊的原始状态--------------");
        set.forEach(e -> {
            System.out.println(e.getName() + " is " + e.getState());
        });

        System.out.println("--------------狼的第一次--------------");
        wolf.cry(WolfState.HAPPY);
        set.forEach(e -> {
            System.out.println(e.getName() + " is " + e.getState());
        });

        System.out.println("--------------狼的第二次--------------");
        wolf.cry(WolfState.HUNGRY);
        set.forEach(e -> {
            System.out.println(e.getName() + " is " + e.getState());
        });
    }
}
